const App = getApp();
const dateTimePicker = require('../../utils/datePicker.js')
let qCloud = require("../../index.js");
Component({
  options: {
    addGlobalClass: true,
    imgPath: qCloud.config.STATIC_IMAGE_PATH,
  },
  /**
   * 组件的属性列表
   */

  properties: {
    params: {
        type: Object,
        value:{
          placeholder: '',
          startDateTime: '',
          endDateTime: '',
          pText: ''
        }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    dateTimeArray: null,
    dateTime: null,
    startDateTime: '',
    endDateTime: '',
    dateTimeWhole: '',
  },
  lifetimes: {
    attached: function () {
     this.setData({
        startDateTime: this.data.params.startDateTime,
        endDateTime: this.data.params.endDateTime
      })
      this.initData()
    }
  },
  pageLifetimes: {
    show: function() {
      this.setData({
        startDateTime: this.data.params.startDateTime,
        endDateTime: this.data.params.endDateTime
      })
      this.initData()
    },
    hide: function() {
      // 页面被隐藏
    },
    resize: function(size) {
      // 页面尺寸变化
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    initData(date) {
      // 获取完整的年月日 时分秒，以及默认显示的数组
      this.data.unit = ['年', '月', '日', '时', '分']
      this.data.dateTimePicker = dateTimePicker.newDateTimePicker(this.data.startDateTime, this.data.endDateTime, this.data.params.pText)
      let obj = this.data.dateTimePicker.render();
      let lastArray = obj.dateTimeArray;
      let lastTime = obj.dateTime;
      for (let i = 0; i < lastArray.length; i++) {
        lastArray[i] = lastArray[i].map(m => m + this.data.unit[i])
      }
      this.data.dateTimeArray = lastArray
      this.data.dateTime = lastTime
      this.setData({
        dateTimeArray: this.data.dateTimeArray,
        dateTime: this.data.dateTime
      })
    },
    changeDateTime(e) {
      this.data.dateTime = e.detail.value
      const year = this.data.dateTimeArray[0][this.data.dateTime[0]].replace(/年/, '')
      const month = this.data.dateTimeArray[1][this.data.dateTime[1]].replace(/月/, '')
      const day = this.data.dateTimeArray[2][this.data.dateTime[2]].replace(/日/, '')
       const hour = this.data.dateTimeArray[3][this.data.dateTime[3]].replace(/时/, '')
       const minute = this.data.dateTimeArray[4][this.data.dateTime[4]].replace(/分/, '')
       this.data.dateTimeWhole = `${year}-${month}-${day} ${hour}:${minute}`
     // this.data.dateTimeWhole = `${year}-${month}-${day}`
      this.setData({
        dateTimeWhole: this.data.dateTimeWhole,
      })
      this.triggerEvent('getDateString', this.data.dateTimeWhole)
    },
    changeDateTimeColumn(e) {
      const { column, value } = e.detail
      // this.$set(this.data.dateTime, column, value)
      let dateTimeTemp = 'dateTime['+column+']'
      this.setData({
        [dateTimeTemp]: value
      })
      this.data.dateTimePicker.setValue({ dateTimeArray: this.data.dateTimeArray, dateTime: this.data.dateTime })
      for (let i = 1; i < this.data.dateTime.length; i++) {
        if (column == i - 1) {
          for (let j = i; j < this.data.dateTime.length; j++) {
            // this.$set(this.data.dateTime, j, 0)
            let temp = 'dateTime['+j+']'
            this.setData({
              [temp]: 0
            })
          }
        }
        let arr = this.data.dateTimePicker.dispatch(i).map(m => m + this.data.unit[i])
        // this.$set(this.data.dateTimeArray, i, arr)
        let temp1 = 'dateTimeArray['+i+']'
        this.setData({
          [temp1]: arr
        })
      }
      this.setData({
        dateTimeArray: this.data.dateTimeArray,
        dateTime: this.data.dateTime
      })
    },


  }
})
